#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
  int * totalFails = malloc(1600 * sizeof(int));
  int * totalPasses = malloc(1600 * sizeof(int));
  int theLine = 0;
  for (int i = 1; i < 17; i++) { 
    char line[1024];
    char something[1000];
    //char * command ="./testdominion ";
    char * command= "./testAllCards";
    char * str[5];
    sprintf(str, "%d",i);
    sprintf(something, "%s%s", command,str);
    //strcat(something, command);
    //strcat(something, str);
    //printf(something);
    system(something);
    command ="gcov dominion.c";
    system(command);
    FILE *file = fopen("dominion.c.gcov", "r");
    char* theString;
    int lineNumber =0;
    int actualNumber=0;
    int theNum = 0;
    theLine = 0;
    while( fgets(line,1024,file) ) {
      theString = strtok(line, ":");
      //printf(theString);
      //printf(theString-1);
      theNum = atoi(theString);
      theString = strtok(NULL, ":");
      theLine = atoi(theString);
    }
    FILE *file1 = fopen("dominion.c.gcov", "r");
    FILE *passCheck = fopen("passCheck.txt","r");
    theString = fgets(line, 1024,passCheck);
    //printf(theString);
    if (atoi(theString) == 1) {
      while( fgets(line,1024,file1) ) {
	theString = strtok(line, ":");
	//printf(theString);
	//printf(theString-1);
	theNum = atoi(theString);
	theString = strtok(NULL, ":");
	theLine = atoi(theString);
	if (theNum != 0) {
	  //printf ("fa %d : %d \n", theNum, theLine);
	  totalFails[theLine] += 1;
	} 
	//printf ("f %d : %d \n", theNum, theLine);
      }
      totalFails[theLine + 1] +=1;
      //printf ("fsa %d : %d \n", theNum, theLine);
    }
    if (atoi(theString) == 2) {
      while( fgets(line,1024,file1) ) {
	theString = strtok(line, ":");
	//printf(theString);
	//printf(theString-1);
	theNum = atoi(theString);
	theString = strtok(NULL, ":");
	theLine = atoi(theString);
	if (theNum != 0) {
	  //printf ("pa %d : %d \n", theNum, theLine);
	  totalPasses[theLine] += 1;
	}
	//printf ("p %d : %d \n", theNum, theLine);
      }
      totalPasses[theLine + 1] +=1;
      //printf ("psa %d : %d \n", theNum, theLine);
    }
  }
  FILE * aFile;
  aFile = fopen("statistics.txt", "w");
  float totalFailsNum = (float)totalFails[theLine + 1];
  float totalPassesNum = (float)totalPasses[theLine + 1];
  float rate = 0.0;
  for (int i = 0; i < (theLine + 1); i++) {
    //printf("%d %d %d %d\n", totalFailsNum, totalPassesNum, totalFails[i], totalPasses[i]);
    float num1 = (float) totalFails[i];
    float num2 = (float) totalPasses[i];
    rate = ((num1/totalFailsNum) / ((num2/totalPassesNum) + (num1/totalFailsNum)));
    //rate = ((float)(totalFails[i]/totalFailsNum)/ ((float)(totalPasses[i]/totalPassesNum) + (float)(totalFails[i]/totalFailsNum)));
    if (num1 > 0 || num2 > 0) {
      fprintf(aFile,"%d : %f\n", i, rate); 
    }
    //printf("%f : %f %f %f %f\n", i, num1, num2, totalFailsNum, totalPassesNum);
    //printf("i : %f\n", ((totalFails[i]/totalFails[theLine+1])/(totalPasses[i]/totalPasses[theLine+1] + totalFails[i]/totalFails[theLine+1]))); 
  }
  //free(totalFails);
  //free(totalPasses);
  fclose(aFile);
  return 0;
}
